﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 台签打印
{

    [Serializable]
    public class Setup : IEquatable<Setup>
    {
        public string Text { get; set; }         //规格
        public Size Size { get; set; }           //标签大小
        public float MarginRatio { get; set; }   //边距比例
        public Point Offset { get; set; }        //内容偏移
        public Title Title1 { get; set; }        //抬头 
        public Title Title2 { get; set; }        //落款
        public bool Mirror => Text != null && Text.Contains("*");  //镜像打印
        static bool? _landscape = null;
        public bool? Landscape => _landscape; //打印方向

        public Setup()
        {
            MarginRatio = 0.15f; //默认保留15%            
            Offset = new Point();
            Title1 = new Title();
            Title2 = new Title();
        }

        public bool Equals(Setup other) => Text == other.Text;
        public override bool Equals(object obj) => (obj is Setup o) && Equals(o);
        public override int GetHashCode() => Text.GetHashCode();
        public override string ToString() => $"{Text},{Size},{MarginRatio},{Offset}\r\n{Title1}\r\n{Title2}";

        public static Setup Parse(string text)
        {
            //规格匹配    竖向Portrait 横向Landscape
            var match = Regex.Match(text, "^(\\d+)[*|x](\\d+)mm(-[PL])?$");
            if (match.Success)
            {
                //规格宽高
                var Width = int.Parse(match.Groups[1].Value);
                var Height = int.Parse(match.Groups[2].Value);
                if (Width >= 20 && Height >= 20 && Width <= 300 && Height <= 300)
                {
                    return new Setup() { Text = text, Size = new Size(Width, Height) };
                }
            }
            return null;
        }

        /// <summary>
        /// 矩形排列计算
        /// </summary>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Rectangle> GetRectangles(Size pageSize)
        {
            _landscape = null;  //打印页方向 null为自动，-P纵向，-L横向
            if (Regex.IsMatch(Text, "-[PL]")) _landscape = Text.EndsWith("-L");
            var size = new Size(Size.Width, Size.Height * (Mirror ? 2 : 1)); //镜像模式高度x2
            return RectangleArranger.Calculate(pageSize, size.Millimeter2Inch(), ref _landscape);
        }
        
    }

    [Serializable]
    public class Title
    {
        public bool Enabled { get; set; }            //启用
        public string Content { get; set; }          //自定义数据
        public string FontName { get; set; }         //字体
        public float FontSize { get; set; }          //字体大小
        public MyFontStyle MyFontStyle { get; set; }   //字体样式
        public int Argb { get; set; }                //颜色
        public Point Point { get; set; }             //位置

        public Font Font1 => new Font(FontName, FontSize, (FontStyle)MyFontStyle);  //字体
        public Color Color1 => Color.FromArgb(Argb);  //颜色

        public Title()
        {
            FontName = "黑体";
            FontSize = 20;
            MyFontStyle = MyFontStyle.常规;
            Argb = Color.Black.ToArgb();
            Point = new Point();
        }

        public override string ToString()
        {
            return $"{Enabled},{FontName},{FontSize},{MyFontStyle},{Argb},{Point},{Content}";

        }
    }
}
